﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TopicPanel : MonoBehaviour
{
    Transform ToggleListArea;

    ToggleGroup toggleGroup;

    Button NextBtn;
    Button LastBtn;
    Button CloseBtn;
    Button UpLoadBtn;

    Text TopicTytleText;
    public TopicItemDM tempTopic;
    int TopicCount = 0;
    string TytleText;
    public List<GameObject> TopictItemBtnList = new List<GameObject>();
    public List<ToggleItemBtn> toggleItemBtns = new List<ToggleItemBtn>();
    public bool IsTrue = true;
    Text RealText;

    private void Awake()
    {
        RealText = ToolUnity.FindGameObject(gameObject, "RealText").GetComponent<Text>();
        TopicTytleText = ToolUnity.FindGameObject(gameObject, "TopicTytleText").GetComponent<Text>();
        ToggleListArea = ToolUnity.FindGameObject(gameObject, "ToggleListArea").transform;
        CloseBtn = ToolUnity.FindGameObject(gameObject, "CloseBtn").GetComponent<Button>();
        UpLoadBtn = ToolUnity.FindGameObject(gameObject, "UpLoadBtn").GetComponent<Button>();
        toggleGroup = ToggleListArea.GetComponent<ToggleGroup>();
        NextBtn = ToolUnity.FindGameObject(gameObject, "NextBtn").GetComponent<Button>();
        LastBtn = ToolUnity.FindGameObject(gameObject, "LastBtn").GetComponent<Button>();
    }

    /// <summary>
    /// 判断是否是正确答案
    /// </summary>
    /// <param name="Msg"></param>
    /// <returns></returns>
    private bool IsRealAnswer(string Msg, TopicItemDM topicItemDM)
    {
        for (int j = 0; j < topicItemDM.RealAnswerList.Count; j++)
        {
            if (Msg == topicItemDM.RealAnswerList[j])
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 判断这道题是否是正确的
    /// </summary>
    /// <returns></returns>
    private bool IsCorrect()
    {
        for (int i = 0; i < toggleItemBtns.Count; i++)
        {
            if (toggleItemBtns[i].GetToggle())
            {
                //if (IsFalseAnswer(toggleItemBtns[i].GetTytle(), tempTopic))
                //{
                //    return false;
                //}
            }
            else
            {
                if (IsRealAnswer(toggleItemBtns[i].GetTytle(), tempTopic))
                {
                    return false;
                }
            }
        }
        return true;
    }

    public string GetTytleText()
    {
        return TytleText;
    }

    void Start()
    {
        UpLoadBtn.onClick.AddListener(() =>
        {
            EzAssetManager.Instance.CreateObjFromRes("TipPanel", transform);
        });

        GetComponent<Button>().onClick.AddListener(() =>
        {
            //ToolUnity.Destroy(gameObject);
            Debug.Log("点击了背景");
        });

        NextBtn.onClick.AddListener(() =>
        {
            OnNextBtn();
        });

        LastBtn.onClick.AddListener(() =>
        {
            OnLastBtn();
        });

        CloseBtn.onClick.AddListener(() =>
        {
            Destroy(gameObject);
        });
    }

    private void OnDestroy()
    {
        tempTopic.IsTrue = IsCorrect();
    }

    public void OnNextBtn()
    {

        if (IsCorrect())
        {
            tempTopic.IsTrue = true;
        }
        else
        {
            tempTopic.IsTrue = false;
        }
        TopicSystem.Instance.ShowNextTopicPanel();
    }

    public void OnLastBtn()
    {
        if (IsCorrect())
        {
            tempTopic.IsTrue = true;
        }
        else
        {
            tempTopic.IsTrue = false;
        }
        TopicSystem.Instance.ShowLastTopicPanel();
    }

    /// <summary>
    /// 设置数据
    /// </summary>
    /// <param name="topicItemDM"></param>
    public void SetData(TopicItemDM topicItemDM)
    {
        Debug.Log("显示答题面板关闭任务信息");
        string strTemp = "";
        tempTopic = topicItemDM;
        TytleText = topicItemDM.TopicTytle;

        if (topicItemDM != null)
        {
            DestoryTopictItemBtnList();
            DuoXuan(topicItemDM);
        }
        else
        {
            Debug.Log("topicItemDM为空");
        }

        if (topicItemDM.Interactable == false)
        {
            if (IsCorrect())
            {
                strTemp += "回答正确";
                RealText.color = Color.green;
                RealText.text = strTemp;
                tempTopic.IsTrue = true;
            }
            else
            {
                strTemp += "回答错误,正确答案为：";
                for (int i = 0; i < tempTopic.RealAnswerList.Count; i++)
                {
                    strTemp += tempTopic.RealAnswerList[i] + "；";
                }
                RealText.color = Color.red;
                RealText.text = strTemp;
                tempTopic.IsTrue = false;
            }
        }
    }

    /// <summary>
    /// 判断是单选还是多选
    /// </summary>
    /// <param name="topicItemDM"></param>
    /// <returns></returns>
    private bool IsDanXuan(TopicItemDM topicItemDM)
    {
        if (topicItemDM.RealAnswerList.Count > 1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    private void DuoXuan(TopicItemDM topicItemDM)
    {
        for (int i = 0; i < topicItemDM.AnswerList.Count; i++)
        {
            GameObject objtemp = EzAssetManager.Instance.CreateObjFromRes("ToggleItemBtn", ToggleListArea);
            bool TempBool = false;
            for (int j = 0; j < topicItemDM.PlayAnswerList.Count; j++)
            {
                if (topicItemDM.PlayAnswerList[j] == topicItemDM.AnswerList[i])
                {
                    TempBool = true;
                }
            }
            ToggleItemBtn toggleItemBtn = objtemp.GetComponent<ToggleItemBtn>();
            toggleItemBtn.SetData(this, topicItemDM.AnswerList[i], TempBool);
            objtemp.GetComponent<Toggle>().interactable = topicItemDM.Interactable;
            TopictItemBtnList.Add(objtemp);
            toggleItemBtns.Add(toggleItemBtn);
            if (IsDanXuan(topicItemDM))
            {
                objtemp.GetComponent<Toggle>().group = toggleGroup;
            }
        }
        if (IsDanXuan(topicItemDM))
        {
            TopicTytleText.text = "[单选] " + topicItemDM.TopicTytle;//题干
        }
        else
        {
            TopicTytleText.text = "[多选] " + topicItemDM.TopicTytle;//题干
        }
    }

    private void DestoryTopictItemBtnList()
    {

        for (int i = 0; i < TopictItemBtnList.Count; i++)
        {
            Destroy(TopictItemBtnList[i]);
        }

        for (int i = 0; i < toggleItemBtns.Count; i++)
        {
            Destroy(toggleItemBtns[i]);
        }
    }
}
